﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Sunny.Lib
{
    public class QueryCondition
    {
        public string FieldName { get; set; }
        public object ReferenceValue { get; set; }
        public CompareType CompareType { get; set; }

        public string GetConditionStr()
        {
            string condition = string.Empty;
            if (string.IsNullOrEmpty(FieldName) || ReferenceValue == null || string.IsNullOrEmpty(CompareType.ToString()))
            {
                return condition;
            }
            else
            {
                switch (CompareType)
                {
                    case Lib.CompareType.Contain:
                        condition = string.Format("{0} like '%{1}%'", FieldName, ReferenceValue);
                        break;
                    case Lib.CompareType.Equal:
                        if (ReferenceValue.GetType().FullName.Equals("System.String", StringComparison.OrdinalIgnoreCase))
                        {
                            condition = string.Format("{0} == '{1}'", FieldName, ReferenceValue);
                        }
                        else
                        {
                            condition = string.Format("{0} == {1}", FieldName, ReferenceValue);
                        }
                        break;
                    case Lib.CompareType.Equal1:
                        if (ReferenceValue.GetType().FullName.Equals("System.String", StringComparison.OrdinalIgnoreCase))
                        {
                            condition = string.Format("{0} = '{1}'", FieldName, ReferenceValue);
                        }
                        else
                        {
                            condition = string.Format("{0} = {1}", FieldName, ReferenceValue);
                        }
                        break;
                    case Lib.CompareType.NotEqual:
                        condition = string.Format("{0} <> '{1}'", FieldName, ReferenceValue);
                        break;
                    case Lib.CompareType.GreatThan:
                        condition = string.Format("{0} > {1}", FieldName, ReferenceValue);
                        break;
                    case Lib.CompareType.LessThan:
                        condition = string.Format("{0} < {1}", FieldName, ReferenceValue);
                        break;
                    case Lib.CompareType.NoGreateThan:
                        condition = string.Format("{0} <= {1}", FieldName, ReferenceValue);
                        break;
                    case Lib.CompareType.NoLessThan:
                        condition = string.Format("{0} >= {1}", FieldName, ReferenceValue);
                        break;
                    case Lib.CompareType.Between:
                        break;
                    case Lib.CompareType.In:
                        condition = string.Format("{0} in ({1})", FieldName, ReferenceValue);
                        break;
                }
            }

            return condition;
        }

        public static string GetWhereStr(List<QueryCondition> qcs, string truth = "1=1")
        {
            string where = truth;

            if (qcs != null && qcs.Count > 0)
            {
                where = String.Join(" and ",
                            (from p in qcs select p.GetConditionStr())
                            );
            }

            return where;
        }
    }

    public enum CompareType
    {
        Equal,          // ==
        Equal1,         // =
        NotEqual,       // !=
        GreatThan,      // >
        NoGreateThan,   // <=
        LessThan,       // <
        NoLessThan,     // >=
        Contain,        //
        Between,        // 1<2<3
        In,             // in (1,2,3)
    }

    public enum ConditionRelationShip
    {
        And,
        Or,
        Group,  // (Condition A and Condition B) or Condition C
    }
}
